package Saas_Exercises;

import java.util.Scanner;

public class Assignment06_RomanConversion {

	public static void main(String[] args) {
		// System.out.println("Please enter your string");
		Scanner sc = new Scanner(System.in);
		String temp = sc.nextLine();

		int strings_to_follow = Integer.parseInt(temp);

		// System.out.println(strings_to_follow);

		String[] original_str = new String[strings_to_follow];
		//
		if (sc.hasNext()) {

			for (int i = 0; i < strings_to_follow; i++) {

				original_str[i] = sc.nextLine();
				// System.out.println(original_str[i]);

				accept_n_evaluate(original_str[i]);
			}
		}

//		 printArr(original_str);

	}

	private static void accept_n_evaluate(String string) { // checks whether the
															// incoming string
															// is a roman or a
															// normal number and
															// then calls the
															// appropriate
															// conversion method

		try {

			int test = Integer.parseInt(string);

			int[] split_num = take_str_return_inarr(string);

			String roman = input_splarr_outroman(split_num);
			System.out.println(roman);

		} catch (Exception e) {

			int final_numeral = roman_to_int(string);
			System.out.println(final_numeral);

		}

	}

	private static int roman_to_int(String string) {

		int[] ori_arr = roman_2intarr(string);

		// printArray(ori_arr);
		int final_numeral = modifyArray(ori_arr);


		return final_numeral;
	}

	private static int modifyArray(int[] ori_arr) {
		int final_numeral = 0;
		for (int i = 0; i < ori_arr.length; i++) {

			if (i < ori_arr.length - 1 && ori_arr[i] < ori_arr[i + 1]) {
				ori_arr[i] = -ori_arr[i];

			}
			// System.out.println(ori_arr[i]);

			final_numeral = final_numeral + ori_arr[i];
		}

		return final_numeral;

	}

	private static int[] roman_2intarr(String string) {

		int[] iarr = new int[string.length()];
		int m = 0;
		for (int i = 0; i < string.length(); i++) {

			char c = string.charAt(i);

		//	System.out.println(c);
			

			switch (c) {
			case 'M':
				m = 1000;

				break;

			case 'D':
				m = 500;

				break;
			case 'C':
				m = 100;

				break;

			case 'L':
				m = 50;

				break;

			case 'X':
				m = 10;

				break;

			case 'I':
				m = 1;

				break;

			}
			iarr[i] = m;
			// System.out.println(iarr[i]);
		}

		return iarr;

	}
//takes in a number(int) array and spits out a Roman number
	private static String input_splarr_outroman(int[] split_num) {

		String s1 = "";
		String s2 = "";
		String s3 = "";
		String s4 = "";

		for (int i = 0; i < split_num.length; i++) {

			int inp = split_num[i];

			// System.out.println(inp);
			if (inp >= 1000 && inp <= 4000) {
				switch (inp) {
				case 1000:
					s1 = "M";
					break;
				case 2000:
					s1 = "MM";
					break;
				case 3000:
					s1 = "MMM";
					break;

				}

			} else if (inp >= 100 && inp <= 900) {
				switch (inp) {
				case 100:
					s2 = "C";
					break;
				case 200:
					s2 = "CC";
					break;
				case 300:
					s2 = "CCC";
					break;
				case 400:
					s2 = "CD";
					break;
				case 500:
					s2 = "D";
					break;
				case 600:
					s2 = "DC";
					break;
				case 700:
					s2 = "DCC";
					break;
				case 800:
					s2 = "DCCC";
					break;
				case 900:
					s2 = "CM";
					break;

				}

			} else if (inp >= 10 && inp <= 90) {
				switch (inp) {
				case 10:
					s3 = "X";
					break;
				case 20:
					s3 = "XX";
					break;
				case 30:
					s3 = "XXX";
					break;
				case 40:
					s3 = "XL";
					break;
				case 50:
					s3 = "L";
					break;
				case 60:
					s3 = "LX";
					break;
				case 70:
					s3 = "LXX";
					break;
				case 80:
					s3 = "LXXX";
					break;
				case 90:
					s3 = "XC";
					break;

				}

			} else if (inp >= 1 && inp <= 9) {

				switch (inp) {
				case 1:
					s4 = "I";
					break;
				case 2:
					s4 = "II";
					break;
				case 3:
					s4 = "III";
					break;
				case 4:
					s4 = "IV";
					break;
				case 5:
					s4 = "V";
					break;
				case 6:
					s4 = "VI";
					break;
				case 7:
					s4 = "VII";
					break;
				case 8:
					s4 = "VII";
					break;
				case 9:
					s4 = "IX";
					break;

				}
			}

		}

		String concat = s1 + s2 + s3 + s4;
		return concat;

	}

	private static int[] take_str_return_inarr(String original_str) {

		String number = original_str;

		int num = Integer.parseInt(number);

		int[] split_nums = new int[number.length()];

		for (int i = 0; i < number.length(); i++) {

			int x = num % 10;
			int multiplier = 10;

			multiplier = (int) Math.pow(multiplier, i);
			x = x * multiplier;
			num = num / 10;

			split_nums[i] = x;

			// System.out.println(split_nums[i]);

		}
		return split_nums;

	}

	private static void printArr(String[] original_str) {

		for (int i = 0; i < original_str.length; i++) {

			System.out.println(original_str[i]);

		}

	}

}
